/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chiabai;

/**
 *
 * @author Oho
 */
import Chiabai_total.LinearTweening;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* 
 * A template for animation applications.
 */
public class AnimatorApplicationTimer extends JFrame
        implements ActionListener {

    int frameNumber = -1;
    Timer timer;
    boolean frozen = false;
    JLabel label;
    JLabel label1;
    
    AnimatorApplicationTimer(int fps, String windowTitle) {
        super(windowTitle);
        int delay = (fps > 0) ? (1000 / fps) : 100;
 //this.label= label;
        
        
        //Set up a timer that calls this object's action handler.
        timer = new Timer(delay, this);
        timer.setInitialDelay(0);
        timer.setCoalesce(true);

        addWindowListener(new WindowAdapter() {
            public void windowIconified(WindowEvent e) {
                stopAnimation();
            }

            public void windowDeiconified(WindowEvent e) {
                startAnimation();
            }

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        label = new JLabel(new ImageIcon("C:/abc/1.jpg"));
        label.setBounds(0, 0, 100, 100);
        label1 = new JLabel(new ImageIcon("C:/abc/2.jpg"));
        label1.setBounds(0, 0, 100, 100);
        begin = label.getX();
        change =destX - begin;
        time=10;
        duration = 1000;
        start =100;
        
        
        
        label.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (frozen) {
                    frozen = false;
                    startAnimation();
                } else {
                    frozen = true;
                    stopAnimation();
                }
            }
        });

        getContentPane().add(label, BorderLayout.CENTER);
    }
    int x = 0, y = 0;

    public void startAnimation() {
        if (frozen) {
            //Do nothing.  The user has requested that we
            //stop changing the image.
        } else {
            //Start animating!
            timer.start();
        }
    }

    public void stopAnimation() {
        //Stop the animating thread.
        timer.stop();
    }
    float begin;
    float change;
    float destX = 1024;
    float time = 0;
    float start = 100;
    float duration = 100;

    public void actionPerformed(ActionEvent e) {
        //Advance the animation frame.
        x++;
        //label.setBounds(x, y, 100, 100);
        
        int x1 = (int) LinearTweening.easeInOutQuad(time, begin, change, duration);
        //              x=LinearTweening.funtionLinearTweening(x, 100, 220, 30) ;
        if(label.getX() >label1.getX())
            label1.setBounds(y, x1-1, 100, 100);
        else
            label.setBounds(x1, y, 100, 100);
        
        //lb.setAlignmentX(x1);
        // sleep(50);
        time += 10;
        y += 10;
        if (time == 1000) {
            time = 0;
            y = 0;
        }

        // frameNumber++;
        // label.setText("Frame " + frameNumber);
    }

    public static void main(String args[]) {
        AnimatorApplicationTimer animator = null;
        int fps = 10;

        // Get frames per second from the command line argument
        if (args.length > 0) {
            try {
                fps = Integer.parseInt(args[0]);
            } catch (Exception e) {
            }
        }
        animator = new AnimatorApplicationTimer(fps, "Animator with Timer");
        animator.pack();
        animator.setVisible(true);
        animator.startAnimation();
    }
}